package tech.lq0.dreamaticvoyage.client;

import net.minecraft.client.Minecraft;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.RenderPlayerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import tech.lq0.dreamaticvoyage.init.ItemRegistry;
import tech.lq0.dreamaticvoyage.item.fourth.hifumi.OminousSickle;
import tech.lq0.dreamaticvoyage.item.second.satou.AncientLollipop;
import tech.lq0.dreamaticvoyage.network.DmvNetwork;
import tech.lq0.dreamaticvoyage.network.packet.OminousWavePacket;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class ClientEventHandler {

    @SubscribeEvent
    public static void setPlayerInvisible(RenderPlayerEvent.Pre event) {
        var otherPlayer = event.getEntity();
        var currentPlayer = Minecraft.getInstance().player;
        if (currentPlayer == null) return;
        if (currentPlayer.equals(otherPlayer)) return;
        if (otherPlayer.isCurrentlyGlowing()) return;

        if (otherPlayer.isInvisibleTo(currentPlayer) && otherPlayer.isInvisible() && otherPlayer.getMainHandItem().getItem() instanceof AncientLollipop) {
            event.setCanceled(true);
        }
    }

    @SubscribeEvent
    public static void onLeftClick(PlayerInteractEvent.LeftClickEmpty event) {
        var player = Minecraft.getInstance().player;
        if (player == null) return;
        if (player.isSpectator()) return;

        ItemStack stack = player.getMainHandItem();
        if (stack.is(ItemRegistry.OMINOUS_SICKLE.get())) {
            int type = OminousSickle.isInvoke(stack) ? 1 : 0;
            float damage = OminousSickle.calculateAttackDamage(stack);
            DmvNetwork.CHANNEL.sendToServer(new OminousWavePacket(type, damage));
        }
    }
}
